/*
 * @Author: your name
 * @Date: 2020-06-06 17:32:14
 * @LastEditTime: 2020-07-09 15:15:20
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \crm\lib\main.dart
 */
import 'dart:io';
import 'package:crm/pages/splash_screen.dart';
import 'package:crm/provider/currentIndex.dart';
import 'package:crm/provider/theme_provider.dart';
import 'package:crm/routers/application.dart';
import 'package:crm/routers/router.dart';
import 'package:crm/utils/sp_util.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:provider/provider.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await SpUtil.getInstance();
  if (Platform.isAndroid) {
    // 以下两行 设置android状态栏为透明的沉浸。写在组件渲染之后，是为了在渲染后进行set赋值，覆盖状态栏，写在渲染之前MaterialApp组件会覆盖掉这个值。
    SystemUiOverlayStyle systemUiOverlayStyle =
        SystemUiOverlayStyle(statusBarColor: Colors.transparent);
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }
  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (context) => CurrentIndexProvider(),
        ),
        ChangeNotifierProvider(
          create: (context) => ThemeProvider(),
        )
      ],
      child: MyApp(),
    ),
  );
}

class MyApp extends StatelessWidget {
  const MyApp({this.theme});
  final ThemeData theme;

  @override
  Widget build(BuildContext context) {
    final router = Router();
    print('theme==============$theme');
    Routers.configureRouter(router);
    Application.router = router;
    return Selector<ThemeProvider, ThemeProvider>(
      selector: (context, provider) => provider,
      shouldRebuild: (pre, next) => true,
      builder: (context, value, child) {
        print(value.getThemeMode());
        return MaterialApp(
          // theme: ThemeData.dark() ,//暗黑模式
          theme: theme ?? value.getTheme(),
          darkTheme: value.getTheme(isDarkMode: true),
          themeMode: value.getThemeMode(),
          title: "张远涛",
          debugShowCheckedModeBanner: false,
          onGenerateRoute: Application.router.generator,
          // theme: ThemeData(
          //   primaryColor: Colors.blue, // 全局修复TextField光标无法与提示文本对齐问题
          //   // textTheme: TextTheme(
          //   //   subhead: TextStyle(textBaseline: TextBaseline.alphabetic),
          //   // ),
          // ),
          home: SplashScreenPage(),
          localizationsDelegates: [
            // 为Material Components库提供本地化的字符串和其他值
            GlobalMaterialLocalizations.delegate,
            // 定义widget默认的文本方向，从左往右或从右往左
            GlobalWidgetsLocalizations.delegate,
            // ChineseCupertinoLocalizations.delegate,
          ],
          supportedLocales: [
            const Locale('zh', 'CH'),
            const Locale('en', 'US'),
          ],
        );
      },
    );
  }
}
